home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10889 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  125 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Boolean evaluation operator
  5. Message-ID: <marnoldDo1CsK.2rK@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4hqcnk$aoj@natasha.rmii.com> <4hte6e$55k@park.interport.net>
  8. Date: Sun, 10 Mar 1996 05:05:08 GMT
  9. Sender: marnold@netcom3.netcom.com
  10.  
  11. yaron@interport.net (Adi) writes:
  12.  
  13. >disch@aartronics.com wrote:
  14.  
  15. >>We have a Boolean class that contains the following operator:
  16.  
  17. >>    // Returns 1 if THIS Boolean Object is false, and 0 if THIS Boolean Object
  18. >>    //  is true.
  19. >>    int
  20. >>    Boolean::operator!(); // No referenced object; operates on THIS
  21.  
  22. >>When used in the following code fragment, compilation fails with the message
  23. >>"Error testbed.cpp 47: Illegal structure operation in function main()":
  24.  
  25. >>    Boolean     bTrue = T;
  26. >>    Boolean     bFalse = F;
  27. >>    if (!bFalse)
  28. >>    {
  29. >>        cout << "bFalse" << endl;
  30. >>    }
  31. >>    if (bTrue)
  32. >>    {
  33. >>        cout << "bTrue" << endl;
  34. >>    }
  35.  
  36. >>Is there a way to define an evaluation operator so that "if (bTrue)" will
  37. >>compile and execute as expected?
  38.  
  39. >>Any input would be greatly appreciated.
  40.  
  41. >>Bob Dischner
  42. >>Aartronics Corp
  43.  
  44. First, a question.  Why this whole compilcated Boolean class anyway?  What's
  45. wrong with the built-in bool type (if you compiler supports it), or the
  46. following temporary emulation until it does...
  47.  
  48.    #define bool  int
  49.    #define true  1
  50.    #define false 0
  51.  
  52. ...??
  53.  
  54. >Try something like this :
  55.  
  56.  
  57. >class Boolean {
  58. >    public :
  59. >        Boolean(unsigned int b = FALSE) 
  60. >            : value(b) {}
  61.  
  62. >        Boolean(const Boolean& b) 
  63. >            : value(b.value) {}
  64.  
  65. >        virtual ~Boolean() {}
  66.  
  67. >        operator unsigned int() 
  68. >        { 
  69. >            return value; 
  70. >        }
  71.  
  72. >        Boolean& operator !() 
  73. >        { 
  74. >            value = !value; return *this; 
  75. >        }
  76.  
  77. Shouldn't this be...
  78.  
  79.                Boolean operator !()
  80.                {
  81.                        Boolean not_value(!value);
  82.  
  83.                        return not_value;
  84.                }
  85.  
  86.  
  87. You certinaly don't want operator!() to change the value of the Boolean
  88. object it's called for!  It should instead return a new Boolean which has 
  89. the opposite state.  With the orignal definiation of opeator!() by Adi,
  90. the statement...
  91.  
  92.     if (!bFalse)
  93.       // ...
  94.  
  95. ...would certinaly branch correctly (the first time), but bFalse would 
  96. continue to be true afterwards, since it's state was changed.  This would
  97. be incorrect.  My definition of operator!() is the correct one.
  98.  
  99. This is similar to what is done with operator+(); it doesn't modify (add
  100. to) the object it's called for, it instead returns a new object, equal to 
  101. the current object plus the other operand.
  102.  
  103. >        Boolean& operator = (const Boolean& t) 
  104. >        { 
  105. >            value = t.value; 
  106. >            return *this;
  107. >        )
  108.  
  109. >    protected :
  110. >        unsigned int value;
  111. >};
  112.  
  113.  
  114.  
  115. >NOTE :
  116.  
  117. >The important thing here is the cast operator to unsigned integer.
  118.  
  119.  
  120.  
  121. >Adi Degani
  122. >New-York, NY
  123.  
  124.  
  125.